﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SC.eWedding.BusinessLogic.Views;
using System.Configuration;
using System.IO;

public partial class Proveedor_Controles_ucFoto : System.Web.UI.UserControl
{
    #region Interface

    public delegate void AgregarBorrarHandler();
    public event AgregarBorrarHandler AgregarBorrar;

    #endregion

    #region Propiedades Privadas

    private List<FotoDet> ListFotoDet
    {
        get
        {
            if (Session["FotoDet"] == null)
                Session["FotoDet"] = new List<FotoDet>();
            return (List<FotoDet>)Session["FotoDet"];
        }
        set { Session["FotoDet"] = value; }
    }

    #endregion

    #region Metodos Privados

    private void Validar()
    {
        if (fuFoto.HasFile)
        {
            List<string> fileExts = new List<string>(new string[] { ".bmp", ".gif", ".ico", ".icon", ".jpg", ".jpeg", ".png", ".tif", ".tiff" });
            FileInfo fi = new FileInfo(fuFoto.FileName);

            if (!fileExts.Contains(fi.Extension.ToLower()))
                throw new Exception("El archivo seleccionado no es una imagen valida");

            if (ListFotoDet.Exists(delegate(FotoDet fd) { return fd.Path.Contains(fuFoto.FileName); }))
                throw new Exception("La imagen: " + fuFoto.FileName + " ya ha sido cargada");
        }
    }

    #endregion

    #region Eventos

    protected void lbAgregar_Click(object sender, EventArgs e)
    {
        if (fuFoto.HasFile)
        {
            try
            {
                Validar();

                string tempPath = ConfigurationManager.AppSettings["temp"] + Session.SessionID + @"\";

                if (!Directory.Exists(tempPath))
                    Directory.CreateDirectory(tempPath);
                fuFoto.SaveAs(tempPath + fuFoto.FileName);

                FotoDet fo = new FotoDet();

                fo.Id = -ListFotoDet.Count;
                fo.Descripcion = txtDescricion.Text;
                fo.Path = tempPath + fuFoto.FileName;
                ListFotoDet.Add(fo);

                fuFoto.Dispose();
                lblMensage.Text = "";
            }
            catch (Exception ex)
            {
                lblMensage.Text = ex.Message;
            }
        }

        txtDescricion.Text = "";

        if (AgregarBorrar != null)
            AgregarBorrar();
    }

    #endregion
}
